package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishDto;
import com.itheima.pojo.DishFlavor;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishFlavorService flavorService;

    @GetMapping("/page")
    public R<IPage> page(Integer page,Integer pageSize,String name){
        LambdaQueryWrapper<Dish> lqw=new LambdaQueryWrapper<>();
        lqw.like(name!=null,Dish::getName,name);
        lqw.orderByAsc(Dish::getUpdateTime);
        IPage<Dish> page1=new Page<>(page,pageSize);
        IPage<Dish> page2 = dishService.page(page1,lqw);
        IPage<DishDto> page3=new Page<>();
        BeanUtils.copyProperties(page2,page3,"records");
        List<Dish> list = page2.getRecords();
        List<DishDto> list1=  list.stream().map((item)->{
        DishDto dishDto=new DishDto();
        BeanUtils.copyProperties(item,dishDto);
            Long categoryId = item.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            if (byId!=null){
                String name1 = byId.getName();
                dishDto.setCategoryName(name1);
            }
            return dishDto;
        }).collect(Collectors.toList());
        page3.setRecords(list1);

//        Iterator<DishDto> iterator = list1.iterator();
//        while(iterator.hasNext()){
//            DishDto next = iterator.next();
//            Long categoryId = next.getCategoryId();
//            Category byId = categoryService.getById(categoryId);
//            String name1 = byId.getName();
//            next.setCategoryName(name1);
//        }
        log.info("最后的集合list是:{}",list1);
        log.info("最后的集合page是:{}",page3);
        return R.success(page3);
    }
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        log.info("新增餐品传来的数据是：{}",dishDto);
        Integer integer = dishService.saveWithFlavor(dishDto);
        if (integer>0){
            return R.success("保存成功");
        }
        else {
            return R.error("保存失败");
        }
//        DishFlavor flavor=dish.
//        log.info("新增餐品传来的参数：{}",dish);
//        log.info("新增餐品传来的口味：{}",flavor);

    }
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        log.info("新增餐品传来的数据是：{}",dishDto);
        Integer integer = dishService.updateWithFlavor(dishDto);
        if (integer>0){
            return R.success("修改成功");
        }
        else {
            return R.error("修改失败");
        }
//        DishFlavor flavor=dish.
//        log.info("新增餐品传来的参数：{}",dish);
//        log.info("新增餐品传来的口味：{}",flavor);

    }
    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable Long id){
        Dish byId = dishService.getById(id);
        DishDto dishDto=new DishDto();
        BeanUtils.copyProperties(byId,dishDto);
        LambdaQueryWrapper<DishFlavor> lqw=new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,dishDto.getId());
        List<DishFlavor> list = flavorService.list(lqw);
        dishDto.setFlavors(list);
        if (dishDto!=null){
            return R.success(dishDto);
        }else {
            return R.error("没有查到");
        }
    }
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
    LambdaQueryWrapper<Dish> lqw=new LambdaQueryWrapper<>();
    lqw.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        List<Dish> list = dishService.list(lqw);
        List<DishDto> list1=list.stream().map((item)->{
            DishDto dishDto=new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Long categoryId = item.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            if(byId!=null){
                String name = byId.getName();
                dishDto.setCategoryName(name);
            }
            Long id = item.getId();
            LambdaQueryWrapper<DishFlavor> lqw1=new LambdaQueryWrapper<>();
            lqw1.eq(DishFlavor::getDishId,id);
            List<DishFlavor> list2 = flavorService.list(lqw1);
            dishDto.setFlavors(list2);
            return dishDto;
        }).collect(Collectors.toList());
        return R.success(list1);
    }

}
